<?php
/**
 * WordPress Admin UI helper for plugin options
 */
class WPUI {
	var $name;
	var $description;
	var $namespace;
	var $options;
	var $optionKeys;

	function WPUI($pluginName, $pluginDescription, $pluginNamespace, $pluginOptions=array(), $pluginOptionKeys=array()) {
		$this->name = $pluginName;
		$this->description = $pluginDescription;
		$this->namespace = $pluginNamespace;
		$this->options = $pluginOptions;
		$this->optionKeys = $pluginOptionKeys;
	}
	
	function renderError() {
		echo '<center><strong>Unauthorized Access! Only Administrators can modify plugin settings.</strong></center>';
	}

	function render($directRender=false) {

		// start dialog
		$ui = sprintf("<div class=\"wrap\">\n<h2>%s Options</h2>\n<p>%s</p>\n<form method=\"post\" action=\"options.php\">%s", 
						$this->name, $this->description, wp_nonce_field('update-options')); 

		// render items
 		$ui .= $this->getRows();
					
		// end dialog
		$ui .= sprintf("<p class=\"submit\">\n
			%s%s
			<input type=\"submit\" name=\"Submit\" value=\"Save Changes\" class=\"button\" />
			</form>
			</p>
			<br /><br />
			<div style=\"float:right\">
				<form action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">
					<input type=\"hidden\" name=\"cmd\" value=\"_s-xclick\">
					<input type=\"image\" src=\"https://www.paypal.com/en_US/i/btn/x-click-but04.gif\" border=\"0\" name=\"submit\" alt=\"Make payments with PayPal - it's fast, free and secure!\">
					<img alt=\"\" border=\"0\" src=\"https://www.paypal.com/en_US/i/scr/pixel.gif\" width=\"1\" height=\"1\">
					<input type=\"hidden\" name=\"encrypted\" value=\"-----BEGIN PKCS7-----MIIHRwYJKoZIhvcNAQcEoIIHODCCBzQCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJ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TOJkh+tXBL+7GmAqUKd95DYXWRM2IcWnqGedaYA894GLeuMiSDHcfPiOlauctTEEtZY/4qw19p87cDCLcpx1lt1xHH1+6CeCSwQ==-----END PKCS7-----\" \>
				</form>
			</div>
			<div style=\"float:right;vertical-align:middle;padding-right:4px\">
				If you derive some benefit from this website/code, please think about making a donation.<br /> Thanks!
			</div>
		</div>", 
		$this->getFormField('hidden', 'action', 'update'), 
		$this->getFormField('hidden', 'page_options', join(',', $this->optionKeys)) );
	  
		// handle output method
		if ($directRender) {
			echo $ui;
		} else {
			return $ui;
		}
	}
	
	function getRows() {
		$rows = '';
		if ($this->options != null && count($this->options) > 0) {
			foreach ($this->options as $title=>$fields) {
				$rows .= sprintf("<h3>%s</h3>\n<table class=\"form-table\">\n", $title);
				foreach ($fields as $field) {
					$rows .= sprintf("<tr valign=\"top\">\n<th scope=\"row\">%s</th>\n<td>%s%s</td>\n</tr>\n",
							$field['label'], 
							$this->getFormField($field['type'], $field['name'], $field['value'], $field['size']),
							((!empty($field['tooltip']))? '<br />'.$field['tooltip'] : '')
							);
				}
				$rows .= "</table>\n";
			}
		}

		return $rows;
	}
	
function getFormField($type, $name, $value, $size=60, $selected=false) {
		$field = '';
		
		// handle form types
		if ($type == 'checkbox') {
			$checked = ($selected) ? ' checked="checked"' : '';
			$field = sprintf("<input type=\"checkbox\" id=\"%s\" name=\"%s\" value=\"%s\"%s />\n", $name, $name, $value, $checked);
		}
		else if ($type == 'html') {
			$field = $value;
		}
		else {
			// here fals type=text,hidden,password...
			$field = $this->getInputField($type, $name, $value, $size);
		}
		
		return $field;
	}

	function getInputField($type, $name, $value, $size=60) {
		$field = '';
		if ($type == 'hidden') {
			$field = sprintf("<input type=\"%s\" id=\"%s\" name=\"%s\" class=\"code\" value=\"%s\" />\n", $type, $name, $name, $value);
		} else {
		 	$field = sprintf("<input type=\"%s\" id=\"%s\" name=\"%s\" class=\"code\" value=\"%s\" size=\"%s\" />\n", $type, $name, $name, $value, $size);
		}
		return $field;
	}
}

// end of class
?>